<?php
/**
 * Zym Framework Demo
 *
 * LICENSE
 *
 * This source file is subject to the new BSD license that is bundled
 * with this package in the file LICENSE.txt.
 *
 * @copyright  Copyright (c) 2008 Zym. (http://www.zym-project.com/)
 * @license http://www.zym-project.com/license New BSD License
 */

/**
 * @see Zym_Controller_Action_Abstract
 */
require_once 'Zym/Controller/Action/Abstract.php';

/**
 * @author Geoffrey Tran
 * @license http://www.zym-project.com/license New BSD License
 * @copyright  Copyright (c) 2008 Zym. (http://www.zym-project.com/)
 */
class Default_LogsController extends Zym_Controller_Action_Abstract
{
    /**
     * Contexts
     *
     * @var unknown_type
     */
    public $contexts = array(
        'view' => array('json')
    );

    /**
     * Init
     *
     * @return void
     */
    public function init()
    {
        $cache = Zym_Cache::factory();
        if (!$cache->load('logs_zftalk_dev')) {
            $logs = new Logs();

            try {
                $logs->import('#zftalk.dev');
                $cache->save(true, null, array(), 600); // save 10 mins
            } catch (Zend_Http_Client_Adapter_Exception $e) {
                // Throw only if in production when we can't import
                if (Zym_App::getInstance()->getEnvironment() == Zym_App::ENV_PRODUCTION) {
                    throw $e;
                }
            }
        }
    }

    /**
     * Index
     *
     * @return void
     */
    public function indexAction()
    {
    }

    /**
     * Search
     *
     * @return void
     */
    public function searchAction()
    {
        $form       = $this->getHelper('Form');
        $searchForm = $form->create('Logs_Search');
        $request    = $this->getRequest();
        $page       = (int) $request->getParam('page', 1);

        if ($request->getParam('query') && $searchForm->isValid($request->getParams())) {
            $logs      = new Logs();
            $query     = $searchForm->getValue('query');
            $paginator = $logs->search($query);
            $paginator->setCurrentPageNumber($page);

            $this->getView()->assign(array(
                'paginator' => $paginator
            ));
        }

        $searchForm->populate($request->getParams());

        // View
        $this->getView()->assign(array(
            'form' => $searchForm
        ));
    }

    /**
     * View data
     *
     * @return void
     */
    public function viewAction()
    {
        $request = $this->getRequest();
        $channel = $request->getParam('channel');
        $year    = $request->getParam('year');
        $month   = $request->getParam('month');
        $day     = $request->getParam('day');
        $page    = $request->getParam('page', 1);

        $vr   = $this->getHelper('ViewRenderer');
        $logs = new Logs();
        $data = array();

        try {
            // We leave off # from the channel for pretty urls
            $channel = ($channel) ? '#' . $channel : $channel;

            if (!$channel) {
                $data = $logs->getChannels();
                $vr->setScriptAction('view-channel');
            }

            if ($channel && !$day) {
                $data = $logs->getDates($channel);

                if (!count($data)) {
                    throw new Exception('Channel not found');
                }

                if (!empty($year) && !$month) {
                    $data = $data[$year];
                }

                if ($year && $month) {
                    $data = $data[$year][$month];
                }
                $vr->setScriptAction('view-dates');
                $this->getView()->assign(array(
                    'channel' => $channel,
                    'year'    => $year,
                    'month'   => $month,
                    'day'     => $day
                ));
            }

            if ($channel && $year && $month && $day) {
                $messages = $logs->getMessagesByDay($channel, $year, $month, $day);
                $messages->setCurrentPageNumber($page);
                $data = $messages;

                $this->getView()->assign(array(
                    'channel' => $channel,
                    'year'    => $year,
                    'month'   => $month,
                    'day'     => $day
                ));
            }
        } catch (Exception $e) {
            // Throw 404
            throw new Zym_Controller_Action_Exception($e->getMessage());
        }

        // View
        $this->getView()->assign(array(
            'data' => $data
        ));
    }
}